package org.jeecg.chase.common.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author leikai
 * @version 1.0
 * @description: 反射辅助类
 * @date 2021/07/23/0023 16:31
 */
public class InvokeSupportUtils {

    /**
     * 根据类名获取类
     *
     * @param className
     * @return java.lang.Class<?>
     * @author leikai
     * @date 2021/07/26/0026 16:52
     */
    public static Class<?> loadClass(String className) {
        try {
            return Class.forName(className);
        } catch (Exception e) {
            // TODO 自动生成的 catch 块
            throw new RuntimeException("没有找到类：" + className);
        }
    }

    /**
     * 通过反射，根基methodName获取method对象
     *
     * @param clazz
     * @param methodName
     * @return java.lang.reflect.Method
     * @author leikai
     * @date 2021/07/26/0026 17:36
     */
    public static Method getMethod(Class<?> clazz, String methodName) {
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                return method;
            }
        }
        Class<?> superClass = clazz.getSuperclass();
        if (null == superClass) {
            return null;
        } else {
            return getMethod(superClass, methodName);
        }
    }

    /**
     * 调用invok方法来调用
     *
     * @param method
     * @param obj
     * @param args
     * @return java.lang.Object
     * @author leikai
     * @date 2021/07/26/0026 17:38
     */
    public static Object invokMethod(Method method, Object obj, Object... args) {
        try {
            method.setAccessible(true);
            Object result = method.invoke(obj, args);
            return result;
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e.getTargetException());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("无法访问" + method + ":" + e.getMessage());
        }
    }

    /**
     * 设置Object属性值
     *
     * @param obj
     * @param fieldName
     * @param value
     * @return void
     * @author leikai
     * @date 2021/07/27/0027 11:33
     */
    public static void setValue(Object obj, String fieldName, Object value) {
        fieldName = fieldName.trim();

        String methodName = getMethodNameByFieldNameAndPrefix(fieldName, "set");
        Method method = getMethod(obj.getClass(), methodName);

        if (null == method) {
            throw new RuntimeException("找不到" + obj.getClass().getSimpleName() + "的" + fieldName + "对应的get方法");
        }
        invokMethod(method, obj, value);
    }

    /**
     * 获取Object属性值
     *
     * @param obj
     * @param fieldName
     * @return java.lang.Object
     * @author leikai
     * @date 2021/07/27/0027 11:33
     */
    public static Object getValue(Object obj, String fieldName) {
        fieldName = fieldName.trim();

        String methodName = getMethodNameByFieldNameAndPrefix(fieldName, "get");
        Method method = getMethod(obj.getClass(), methodName);

        if (null == method) {
            throw new RuntimeException("找不到" + obj.getClass().getSimpleName() + "的" + fieldName + "对应的get方法");
        }
        return invokMethod(method, obj);
    }

    public static Map<String, Object> getValueMap(Object object) {
        Class<?> clazz = object.getClass();

        Map<String, Object> valueMap = new HashMap<String, Object>();
        Field[] declaredFields = clazz.getDeclaredFields();
        List<Field> fields = Arrays.asList(declaredFields);
        fields.forEach(field -> {
            String fieldName = field.getName();
            field.setAccessible(true);
            Object value = getFieldValue(field, object);
            valueMap.put(fieldName, value);
        });
        return valueMap;
    }

    public static Object getFieldValue(Field field, Object obj) {
        try {
            field.setAccessible(true);
            return field.get(obj);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("无法访问" + field.getType().getSimpleName() + "的" + field.getName());
        }
    }

    public static String getMethodNameByFieldNameAndPrefix(String fieldName, String prefix) {
        String methodName = fieldName.replaceFirst(fieldName.substring(0, 1), fieldName.substring(0, 1).toUpperCase());
        return prefix + methodName;
    }

    public static Field getField(Class<?> clazz, String fieldName) {
        Field field = null;
        try {
            field = clazz.getDeclaredField(fieldName);
        } catch (Exception e) {

        }

        if (null == field) {
            Class superclass = clazz.getSuperclass();
            if (null == superclass) {
                throw new RuntimeException("无法访问" + clazz.getSimpleName() + "的" + fieldName);
            } else {
                return getField(superclass, fieldName);
            }
        } else {
            return field;
        }

    }
}
